package com.wl.shixun5.dao;

import com.wl.shixun5.util.Bookjdbc_Util;
import com.wl.shixun5.info.BuyerInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 会员信息DAO类
 */
public class BuyerInfo_DAO {
    public static BuyerInfo buyer = new BuyerInfo(); // 可以把用户信息传递出去

    /**
     * 验证会员登录
     * @param ID 会员代号
     * @param pwd 密码
     * @return 是否验证成功
     */
    public static boolean isMember(String ID, String pwd) {
        boolean r = false;
        String sql = "select * from buyerinfo where memberID=? and pwd=?";
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setString(1, ID);
            ps.setString(2, pwd);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                r = true;
                buyer.setAddress(rs.getString("address"));
                buyer.setEmail(rs.getString("email"));
                buyer.setId(rs.getInt("Id"));
                buyer.setLoginTimes(rs.getInt("loginTimes"));
                buyer.setMemberID(ID);
                buyer.setMembername(rs.getString("membername"));
                buyer.setPhoneCode(rs.getString("phoneCode"));
                buyer.setPwd(pwd);
                buyer.setZipcode(rs.getString("zipcode"));
                sql = "Update buyerinfo set loginTimes = loginTimes + 1"
                    + " where memberID =?";
                ps = cnn.prepareStatement(sql);
                ps.setString(1, ID);
                ps.executeUpdate();
            }
            if (rs != null) {rs.close();}
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return r;
    }
}
